home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1152 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  62 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: clamage@Eng.sun.com (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Just one more library function
  5. Date: 18 Apr 1996 15:40:42 GMT
  6. Organization: Sun Microsystems Inc.
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4l5n3v$h7s@engnews1.Eng.Sun.COM>
  9. References: <u93f63214o.fsf@yorick.cygnus.com>
  10. Reply-To: clamage@Eng.sun.com
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. X-Nntp-Posting-Host: taumet.eng.sun.com
  13. Content-Length: 1521
  14. X-Lines: 38
  15. Originator: clamage@taumet
  16.  
  17. In article fsf@yorick.cygnus.com, jason@cygnus.com (Jason Merrill) writes:
  18. >>>>>> Igor Boukanov <Igor.Boukanov@fi.uib.no> writes:
  19. >
  20. >> Why do not require that every C++ implementation should provide function
  21. >> that will return for the pointer obtained from new[] the number of 
  22. >> allocated elements, i.e. something like:
  23. >
  24. >> template<class T> size_t array_allocation_size(T* p);
  25. >
  26. >> So for 'int* p = new int[3];' 'array_allocation_size(p)' will return 3.
  27. >
  28. >> Such function will be very easy to implement because in the case of
  29. >> delete[] compiler use something like this.
  30. >
  31. >Not necessarily.  For instance, g++ only stores the number of elements if 
  32. >
  33. >1) The element type has a destructor, or
  34. >2) The element type has an operator delete that takes the size_t argument.
  35.  
  36. Jason's answer mentions the overhead it would impose on all users whether
  37. they want the feature or not.
  38.  
  39. I'd also like to point out that if you need to query an array about its
  40. size, you probably should not be using a C-style array, but an array
  41. class instead.
  42.  
  43. Finally, the proposal mentions only arrays on the heap. Is it the case that
  44. you care about the size only of arrays allocated with operator new[],
  45. and not static arrays? What about:
  46.     extern T myarray[]; // how big is it?
  47.  
  48. If you use an array class, you can get the functionality you want, and
  49. don't necessarily have to pay for functionality you don't want. If you
  50. use the standard vector class, you don't even have to write any code.
  51. ---
  52. Steve Clamage, stephen.clamage@eng.sun.com
  53.  
  54.  
  55.  
  56.  
  57. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  58. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  59. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  60. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  61. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  62.